using System;
using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using Random = UnityEngine.Random;

public enum BinaryRole
{
    Normal,
    Result,
}

public class BinaryPart : MonoBehaviour
{
    private Riddle parent;
    
    public BinaryRole BinaryRole = BinaryRole.Normal;

    private TextMeshPro label;

    public bool canEdit = false;
    public int result;
    
    private float timing;

    private void Awake()
    {
        parent = GetComponentInParent<Riddle>();
        
        label = GetComponent<TextMeshPro>();
        if (canEdit && BinaryRole != BinaryRole.Result)
        {
            label.text = Random.Range(0, 2).ToString();
        }
        else if (BinaryRole == BinaryRole.Result)
        {
            label.text = result.ToString();
        }
        
        InitEvent();
    }

    private void InitEvent()
    {
#if UNITY_ANDROID
        
#endif
    }

    private void OnDestroy()
    {
#if UNITY_ANDROID
        EasyButton.On_ButtonUp -= OnFuncBtnUp;

#endif
    }

    public string GetText()
    {
        return label.text;
    }
    
    private void OnTriggerStay2D(Collider2D other)
    {
        if (other.gameObject.layer.Equals(9))
        {
            if (canEdit && Input.GetKey(KeyCode.E) && Time.timeScale != 0 && Time.time - timing >= 0.2)
            {
                timing = Time.time;
                label.text = label.text.Equals("1") ? "0" : "1";
                parent.CheckAnswer();
            }
        }
    }

    private void OnFuncBtnUp(string btn_name)
    {
        if (!btn_name.Equals("FuncBtn"))
            return;
        
        label.text = label.text.Equals("1") ? "0" : "1";
        parent.CheckAnswer();
    }
    
    private void OnTriggerEnter2D(Collider2D other)
    {
#if UNITY_ANDROID
        if(other.gameObject.layer.Equals(9)) {
            GameController.Instance().panelCtr.phonePanel.SwitchBtnsActive(true);
            EasyButton.On_ButtonUp += OnFuncBtnUp;
        }
#endif
    }

    private void OnTriggerExit2D(Collider2D other)
    {
#if UNITY_ANDROID
        if(other.gameObject.layer.Equals(9)) {
            GameController.Instance().panelCtr.phonePanel.SwitchBtnsActive(false);
            EasyButton.On_ButtonUp -= OnFuncBtnUp;
        }
#endif
    }
}
